package com.sky.service.Impl;

import com.sky.mapper.UserBehaviorMapper;
import com.sky.mapper.UserMapper;
import com.sky.model.vo.Top10ReportVO;
import com.sky.model.vo.UserReportVO;
import com.sky.model.vo.ViewReportVO;
import com.sky.model.vo.ViewsTop10ReportVO;
import com.sky.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private UserBehaviorMapper userBehaviorMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //当前集合存储从begin到end的所有日期
        List<LocalDate> dateList = new ArrayList();
        dateList.add(begin);
        while (begin.isBefore(end)){
            //日期加1，begin往后一天，直到等于end
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //存放每天新增的用户数量 select count(id) from user where create_time > #{startTime} and creat_time < #{endTime}
        List<Integer> newUserList=new ArrayList<>();
        //存放每天全部的用户数量 select count(id) from user where create_time < #{endTime}
        List<Integer> totalUserList=new ArrayList<>();

        for(LocalDate date : dateList){
            //查询data日期对应的新增用户数量
            LocalDateTime beginTime=LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime=LocalDateTime.of(date, LocalTime.MAX);

            Map map=new HashMap();

            map.put("endTime",endTime);

            Integer totalUser=userMapper.countByMap(map);
            //如果全部用户数量为null，设置为0
            totalUser = totalUser==null?0:totalUser;
            totalUserList.add(totalUser);

            //查询data日期对应的全部用户数量
            map.put("beginTime",beginTime);

            Integer newUser=userMapper.countByMap(map);
            //如果新增用户数量为null，设置为0
            newUser = newUser==null?0:newUser;
            newUserList.add(newUser);
        }
        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))//日期，以逗号分隔
                .newUserList(StringUtils.join(newUserList, ","))//新增用户数量，以逗号分隔
                .totalUserList(StringUtils.join(totalUserList, ","))//全部用户数量，以逗号分隔
                .build();
    }

    /**
     * 浏览量统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ViewReportVO viewStatistics(LocalDate begin, LocalDate end) {
//当前集合存储从begin到end的所有日期
        List<LocalDate> dateList = new ArrayList();
        dateList.add(begin);
        while (begin.isBefore(end)){
            //日期加1，begin往后一天，直到等于end
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
//存放每天新增的浏览量 select count(id) from view where create_time > #{startTime} and creat_time < #{endTime}
        List<Integer> newViewList = new ArrayList<>();
//存放每天全部的浏览量 select count(id) from view where create_time < #{endTime}
        List<Integer> totalViewList = new ArrayList<>();

        for(LocalDate date : dateList) {
            //查询data日期对应的新增浏览量
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap();

            map.put("endTime", endTime);

            Integer totalView = userBehaviorMapper.countViewByMap(map);
            //如果全部浏览量为null，设置为0
            totalView = totalView == null ? 0 : totalView;
            totalViewList.add(totalView);

            //查询data日期对应的全部浏览量
            map.put("beginTime", beginTime);

            Integer newView = userBehaviorMapper.countViewByMap(map);
            //如果新增浏览量为null，设置为0
            newView = newView == null ? 0 : newView;
            newViewList.add(newView);
        }
        return ViewReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))//日期，以逗号分隔
                .newViewList(StringUtils.join(newViewList, ","))//新增访问数量，以逗号分隔
                .totalViewList(StringUtils.join(totalViewList, ","))//全部访问数量，以逗号分隔
                .build();
    }

    /**
     * 浏览量Top10统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public ViewsTop10ReportVO ViewTop10Statistics(LocalDate begin, LocalDate end) {
        // 将LocalDate转换为LocalDateTime
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        // 创建一个Map来存储查询参数
        Map<String, Object> map = new HashMap<>();
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);

        // 查询数据库获取指定日期范围内浏览量前十的记录
        List<Top10ReportVO> top10ViewsList = userBehaviorMapper.selectTop10Views(map);

        List<String>names=top10ViewsList.stream().map(Top10ReportVO::getName).collect(Collectors.toList());
        List<Integer>numbers=top10ViewsList.stream().map(Top10ReportVO::getViewCount).collect(Collectors.toList());
        // 返回结果
        return ViewsTop10ReportVO.builder()
                .nameList(StringUtils.join(names, ","))
                .top10ViewsList(StringUtils.join(numbers, ","))
                .build();
    }
}
